﻿//********************************************************************************//
//创建日期：<创建日期，2016-05-11>
//创建作者：<郭登杰>
//功能说明：来函流程公共控件-右侧底部操作按钮
//********************************************************************************//

using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI;
using BLL;
using BLL.Access;
using DevExpress.Web;
using Entity;
using Web.Class;

namespace Web.Controls.LAIHAN
{
    public partial class UCLHLCBTNS : UserControl, IUCLHOperate
    {
        #region 属性

        /// <summary>
        ///     来函流程
        /// </summary>
        public int LaiHanLC
        {
            get
            {
                if (!hdCollection.Contains("h_laihanlc")) hdCollection.Set("h_laihanlc", -1);
                return Convert.ToInt32(hdCollection.Get("h_laihanlc"));
            }
            set { hdCollection.Set("h_laihanlc", value); }
        }

        /// <summary>
        ///     来函ID
        /// </summary>
        public string LaiHanID
        {
            set
            {
                if (value == null)
                    hdCollection.Remove("h_laihanid");
                else
                    hdCollection.Set("h_laihanid", value);
            }
            get { return hdCollection.Contains("h_laihanid") ? hdCollection.Get("h_laihanid").ToString() : null; }
        }

        /// <summary>
        ///     来函类型
        /// </summary>
        public eLaiHanLX LaiHanLX
        {
            get { return laiHanLX; }
            set
            {
                hdCollection.Set("h_laiHanLX", (int) value);
                laiHanLX = value;
            }
        }

        private eLaiHanLX laiHanLX;

        /// <summary>
        ///     记录来函编号
        /// </summary>
        public string LaiHanBH
        {
            set { hdCollection.Set("h_laihanbh", value); }
            get { return hdCollection.Contains("h_laihanbh") ? hdCollection.Get("h_laihanbh").ToString() : null; }
        }

        /// <summary>
        ///     记录用户编号
        /// </summary>
        public string YongHuH
        {
            set { hdCollection.Set("h_yonghuh", value); }
            get { return hdCollection.Contains("h_yonghuh") ? hdCollection.Get("h_yonghuh").ToString() : null; }
        }

        public bool FSXG
        {
            set { ViewState["SFXG"] = value; }
            get { return ViewState["SFXG"] == null ? false : Convert.ToBoolean(ViewState["SFXG"]); }
        }

        #endregion

        #region 变量

        private readonly YS_LH_LAIHANXXProcess _YS_LH_LAIHANXXBLL = new YS_LH_LAIHANXXProcess();

        private SpHelper _sphelper = new SpHelper();

        /// <summary>
        /// 参数bll
        /// </summary>
        QS_CANSHUBBLL _CANSHUBLL = new QS_CANSHUBBLL();

        #endregion

        #region 事件

        /// <summary>
        ///     页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) InitControl();
        }

        /// <summary>
        ///     实际执行后台操作
        /// </summary>
        protected void btnActPerform_Click(object sender, EventArgs e)
        {
            if (!FSXG)
            {
                //确认或提交操作成功,清空内容并重新绑定数据
                var ucList = Parent.FindControl("UCLHLCLAIHANXX") as UCLHLCLAIHANXX;
                var ucConditions = Parent.FindControl("UCLHLCTOPINFO1") as UCLHLCTOPINFO1;
                ucList.BindList(ucConditions.GuanLiKH, ucConditions.LaiHanBH, ucConditions.YongHuH, ucConditions.YWY,
                    ucConditions.FuShenXG);
                if (ucList.LHCount == 0) Response.Redirect("/Pages/YongShui/14.DaiChuGL/DAICHUL.aspx");
                //if (this.LaiHanLX == eLaiHanLX.月度)
                //{
                //    YUEDUTZLC LHYueDuTZRightInfo = this.Parent.FindControl("YUEDUTZLC") as YUEDUTZLC;
                //    ucList.BindList(null,null,null,null,null);
                //}
                BtnXG.Text = "修改";
            }
        }

        /// <summary>
        ///     响应前台回调
        /// </summary>
        protected void ASPxCallback1_Callback(object source, CallbackEventArgs e)
        {
            var cr = new CallbackResult(e.Parameter);
            ValidateRS rs;
            var lhxx = _YS_LH_LAIHANXXBLL.SelectYS_LH_LAIHANXX(decimal.Parse(LaiHanID));
            var sphelp = new SpHelper();

            if (e.Parameter == "YueTiaoZSY") // 月调整审阅
            {
                int iSLYM = int.Parse(BLL.BusinessTool.YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString()));
                DataTable dt = _CANSHUBLL.SelectQS_CANSHUBsDynamic(string.Format("i_jlzt=0 and S_MINGCHENG='YueTiaoZSY_YM'", iSLYM), null);
                QS_CANSHUB cs = new QS_CANSHUB();
                if (dt.Rows.Count > 0)
                    cs = _CANSHUBLL.SelectQS_CANSHUB(Convert.ToInt32(dt.Rows[0]["ID"]));
                else
                    cs.S_MINGCHENG = "YueTiaoZSY_YM";
                cs.S_ZHI = iSLYM.ToString();
                cs.I_JLZT = 0;
                _CANSHUBLL.InsertOrUpdate(cs);
            }

            if (e.Parameter == "zjxb") //自来水内勤直接销表
            {
                //if (ucList.IsConfirm(item))
                if (lhxx.I_DANGQIANBZQR != 1)
                {
                    cr.Msg = "当前操作来函未确认.";
                    cr.IsSuccess = false;
                    cr.IsAlertMsg = true;
                    e.Result = cr.ToString();
                    return;
                }

                //审批不同意时
                var drcT = new SpHelper().P_YS_LHGETSHENPIYJS((int) lhxx.I_LAIHANLX, lhxx.S_LAIHANBH).Tables[0].Rows;
                if (drcT.Count == 0) throw new Exception("来函信息副表数据异常.请检查数据.");
                var spjg = false;
                for (var i = 0; i < drcT.Count; i++)
                    if (Convert.ToString(drcT[i]["I_LIUCHENGBH"]) == lhxx.I_LIUCHENGBH.ToString())
                        spjg = Convert.ToString(drcT[i]["I_SHENPIJG"]) == "1";
                if (!spjg)
                {
                    cr.Msg = "请选择同意";
                    cr.IsSuccess = false;
                    cr.IsAlertMsg = true;
                    e.Result = cr.ToString();
                    return;
                }

                //获取要销的水表编号
                var drc = new YS_LH_XIAOBIAOSBXXBLL()
                    .SelectYS_LH_XIAOBIAOSBXXesDynamic("S_LAIHANBH=" + lhxx.S_LAIHANBH, null).Rows;
                var shuibiaobhs = new List<string>();
                foreach (DataRow item in drc)
                    shuibiaobhs.Add(item["S_SHUIBIAOBH"].ToString());
                int? rsno = 0;
                var rsmsg = "";
                //判断是否满足三个月无自来水公司数据
                for (var i = 0; i < shuibiaobhs.Count; i++)
                {
                    sphelp.P_YSLH_ZHIJIEXBPD(shuibiaobhs[i], out rsno, out rsmsg);
                    if (rsno < 0) break;
                }

                if (rsno < 0)
                {
                    cr.Msg = rsmsg;
                    cr.IsSuccess = false;
                    cr.IsAlertMsg = true;
                    e.Result = cr.ToString();
                    return;
                }

                //更新来函主信息                        
                lhxx.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
                lhxx.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
                lhxx.I_DANGQIANBZQR = 0;
                lhxx.I_LIUCHENGZT = (int) eLaiHanLCZT.通过;
                _YS_LH_LAIHANXXBLL.InsertOrUpdate(lhxx);
                //执行销表通过信息同步
                sphelp.P_YS_LH_XIAOBIAOSPTGTB(lhxx.S_LAIHANBH, lhxx.S_YONGHUBH,
                    string.Join(",", shuibiaobhs), LoginInfo.CurrentUser.UserID.ToString());
                cr.Msg = "操作成功";
                cr.IsSuccess = true;
                cr.IsAlertMsg = true;
                e.Result = cr.ToString();
                return;
            }

            if (e.Parameter == "zjxh") //自来水内勤直接销户
            {
                //if (ucList.IsConfirm(item))
                if (lhxx.I_DANGQIANBZQR != 1)
                {
                    cr.Msg = "当前操作来函未确认.";
                    cr.IsSuccess = false;
                    cr.IsAlertMsg = true;
                    e.Result = cr.ToString();
                    return;
                }

                //审批不同意时
                var drcT = new SpHelper().P_YS_LHGETSHENPIYJS((int) lhxx.I_LAIHANLX, lhxx.S_LAIHANBH).Tables[0].Rows;
                if (drcT.Count == 0) throw new Exception("来函信息副表数据异常.请检查数据.");
                var spjg = false;
                for (var i = 0; i < drcT.Count; i++)
                    if (Convert.ToString(drcT[i]["I_LIUCHENGBH"]) == lhxx.I_LIUCHENGBH.ToString())
                        spjg = Convert.ToString(drcT[i]["I_SHENPIJG"]) == "1";
                if (!spjg)
                {
                    cr.Msg = "请选择同意";
                    cr.IsSuccess = false;
                    cr.IsAlertMsg = true;
                    e.Result = cr.ToString();
                    return;
                }

                //更新来函主信息                        
                lhxx.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
                lhxx.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
                lhxx.I_DANGQIANBZQR = 0;
                lhxx.I_LIUCHENGZT = (int) eLaiHanLCZT.通过;
                _YS_LH_LAIHANXXBLL.InsertOrUpdate(lhxx);
                //执行销户通过信息同步
                sphelp.P_YSLH_XIAOHUSPTGTB(lhxx.S_LAIHANBH, lhxx.S_YONGHUBH, LoginInfo.CurrentUser.UserID.ToString());
                cr.Msg = "操作成功";
                cr.IsSuccess = true;
                cr.IsAlertMsg = true;
                e.Result = cr.ToString();
                return;
            }

            if (e.Parameter == "queren" || e.Parameter == "tijiao")
            {
                //验证
                rs = LaiHanLCTools.ValidateQueRenOrTiJiao(Parent, e.Parameter);
                if (!rs.IsSuccess)
                {
                    cr.Msg = rs.Msg;
                    cr.IsSuccess = false;
                    cr.IsAlertMsg = true;
                    e.Result = cr.ToString();
                    return;
                }
            }

            //new YS_LH_LAIHANXX();
            var ucShenPi = Parent.FindControl("UCLHLCSHENPI") as UCLHLCSHENPI;
            //UCLHYueDuTZRightInfo YueDuTZRightInfo = this.Parent.FindControl("UCLHYueDuTZRightInfo") as UCLHYueDuTZRightInfo;
            if (e.Parameter == "queren")
            {
                //更新来函主信息
                //lhxx = this._YS_LH_LAIHANXXBLL.SelectYS_LH_LAIHANXX(decimal.Parse(this.LaiHanID));
                var ucList = Parent.FindControl("UCLHLCLAIHANXX") as UCLHLCLAIHANXX;
                lhxx.S_SHENQINGLY = ucList.LaiHanNR;
                lhxx.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
                lhxx.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
                lhxx.I_DANGQIANBZQR = 1;
                _YS_LH_LAIHANXXBLL.InsertOrUpdate(lhxx);
                if (ucShenPi != null) ucShenPi.CommitContent(lhxx, false);
                //if (YueDuTZRightInfo != null)
                //{
                //    YueDuTZRightInfo.SaveQR();
                //}
                cr.Msg = "操作成功";
                cr.IsSuccess = true;
                cr.IsAlertMsg = false;
                e.Result = cr.ToString();
            }
            else if (e.Parameter == "tijiao") //当前逻辑 提交时提交勾选列表中已确认的
            {
                if (ucShenPi != null)
                {
                    var issuccess = false;
                    var ucList = Parent.FindControl("UCLHLCLAIHANXX") as UCLHLCLAIHANXX;
                    foreach (var item in ucList.SelectedLaiHanXX())
                        //if (ucList.IsConfirm(item))
                        if (item.I_DANGQIANBZQR == 1)
                        {
                            //更新来函主信息                        
                            item.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
                            item.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
                            item.I_DANGQIANBZQR = 0;
                            _YS_LH_LAIHANXXBLL.InsertOrUpdate(item);
                            //更新审批内容等
                            ucShenPi.CommitContent(item, true);
                            issuccess = true;
                        }

                    cr.IsSuccess = issuccess;
                    if (issuccess)
                        cr.Msg = "操作成功";
                    else
                        cr.Msg = "没有已确认的来函";
                    cr.IsAlertMsg = true;
                    e.Result = cr.ToString();
                }

                //if (YueDuTZRightInfo != null )
                //{
                //    YueDuTZRightInfo.LaiHanXXTJ(cr);
                //    e.Result=cr.ToString();
                //}                
            }
            else if (e.Parameter == "fstj") //当前逻辑 提交时提交勾选列表中已确认的
            {
                var uc = Parent.FindControl("UCLHLCYIJIANS") as UCLHLCYIJIANS;
                rs = uc.FSXGSaveValidate();
                if (!rs.IsSuccess)
                {
                    cr.Msg = rs.Msg;
                    cr.IsSuccess = false;
                    cr.IsAlertMsg = true;
                    e.Result = cr.ToString();
                    return;
                }

                uc.FSXGSave(lhxx);
                cr.IsSuccess = true;
                cr.IsAlertMsg = true;
                cr.Msg = "操作成功!";
                e.Result = cr.ToString();
            }

            else if (e.Parameter == "plqueren")
            {
                //更新来函主信息
                //lhxx = this._YS_LH_LAIHANXXBLL.SelectYS_LH_LAIHANXX(decimal.Parse(this.LaiHanID));
                var ucList = Parent.FindControl("UCLHLCLAIHANXX") as UCLHLCLAIHANXX;
                DataRowCollection drcT = null;
                var plqrCount = 0;
                foreach (var item in ucList.SelectedLaiHanXX())
                    if (item.I_DANGQIANBZQR != 1)
                    {
                        plqrCount++;
                        //从上一步复制内容作为确认内容
                        if (item.I_LAIHANLX == (int) eLaiHanLX.月度)
                        {
                            var bllT = new YS_LH_YUETIAOZProcess();
                            YS_LH_YUETIAOZ itemFB = null;
                            drcT = bllT.SelectYS_LH_YUETIAOZsDynamic(
                                "I_JLZT=0 AND S_LAIHANBH='" + item.S_LAIHANBH + "'", null).Rows;
                            if (drcT.Count != 1) throw new Exception("来函：" + item.S_LAIHANBH + "未找到。");
                            itemFB = bllT.SelectYS_LH_YUETIAOZ(Convert.ToDecimal(drcT[0]["ID"]));
                            itemFB.S_CAOZUORQM = LoginInfo.CurrentUser.ElectronSign;
                            if (item.I_LIUCHENGBH == (int)eLaiHanLCBH.主任)
                            {
                                itemFB.I_ZHUREN = itemFB.I_YEWUYJY;
                                itemFB.S_JIESHUIKZ = "拟同意。";
                            }
                            if (item.I_LIUCHENGBH == (int) eLaiHanLCBH.节水科)
                            {
                                //本部用户无主任步骤
                                if (item.S_YONGHUBH.Substring(0, 2) == "14")
                                    itemFB.I_JIESHUIKZ = itemFB.I_YEWUYJY;
                                else
                                    itemFB.I_JIESHUIKZ = itemFB.I_ZHUREN;
                                itemFB.S_JIESHUIKZ = "拟同意。";
                            }
                            else if (item.I_LIUCHENGBH == (int) eLaiHanLCBH.计财科)
                            {
                                itemFB.I_JICAIKZ = itemFB.I_JIESHUIKZ;
                                itemFB.S_JIESHUIKZ = "拟同意。";
                            }
                            else if (item.I_LIUCHENGBH == (int) eLaiHanLCBH.处长)
                            {
                                itemFB.I_CHUZHANG = itemFB.I_JICAIKZ;
                                itemFB.S_JIESHUIKZ = "同意。";
                            }

                            bllT.InsertOrUpdate(itemFB);
                        }
                        else if (item.I_LAIHANLX == (int) eLaiHanLX.计划变更)
                        {
                            int I_LIUCHENGBH = (int)item.I_LIUCHENGBH;
                            int I_LIUCHENGBHLast = "51" == item.S_ZHANDIANBH && 50 == I_LIUCHENGBH ? I_LIUCHENGBH - 20 : I_LIUCHENGBH - 10;

                            var bllT = new YS_LH_JIHUABGProcess();
                            drcT = bllT.SelectYS_LH_JIHUABGsDynamic("I_JLZT=0 AND S_LAIHANBH='" + item.S_LAIHANBH + "' and I_LIUCHENGBH = " + I_LIUCHENGBHLast,
                                null).Rows;
                            if (drcT.Count != 1) throw new Exception("records count is not correct");
                            var itemFB = new YS_LH_JIHUABG();
                            itemFB.S_CAOZUORQM = LoginInfo.CurrentUser.ElectronSign;
                            itemFB.D_CAOZUOSJ = DateTime.Now;
                            itemFB.D_SHURURQ = DateTime.Now;
                            itemFB.I_FEIYONGBH = Convert.ToInt32(drcT[0]["I_FEIYONGBH"]);
                            itemFB.I_JIAJIACS = Convert.ToInt32(drcT[0]["I_JIAJIACS"]);
                            itemFB.I_JIAJIAYSL = Convert.ToInt32(drcT[0]["I_JIAJIAYSL"]);
                            itemFB.I_JLZT = 0;
                            itemFB.I_LIUCHENGBH = (int) item.I_LIUCHENGBH;
                            itemFB.I_SHENPIL = Convert.ToInt32(drcT[0]["I_SHENPIL"]);
                            itemFB.I_SHIYONGSL = Convert.ToInt32(drcT[0]["I_SHIYONGSL"]);
                            itemFB.I_SHURUR = LoginInfo.CurrentUser.UserID;
                            itemFB.I_YUANJIHUA = Convert.ToInt32(drcT[0]["I_YUANJIHUA"]);
                            itemFB.I_ZWYM = Convert.ToInt32(drcT[0]["I_ZWYM"]);
                            itemFB.N_HEXIAOJE = Convert.ToDouble(drcT[0]["N_HEXIAOJE"]);
                            itemFB.N_JIAJIAJE = Convert.ToDouble(drcT[0]["N_JIAJIAJE"]);
                            itemFB.N_QIANFEIJE = Convert.ToDouble(drcT[0]["N_QIANFEIJE"]);
                            itemFB.N_SHISHOUJE = Convert.ToDouble(drcT[0]["N_SHISHOUJE"]);
                            itemFB.N_YINGSHOUJE = Convert.ToDouble(drcT[0]["N_YINGSHOUJE"]);
                            itemFB.N_YSJ = Convert.ToDouble(drcT[0]["N_YSJ"]);
                            itemFB.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
                            itemFB.S_LAIHANBH = Convert.ToString(drcT[0]["S_LAIHANBH"]);
                            itemFB.S_SHENPIYJ = Convert.ToString(drcT[0]["S_SHENPIYJ"]);
                            itemFB.S_YONGHUH = Convert.ToString(drcT[0]["S_YONGHUH"]);
                            bllT.InsertOrUpdate(itemFB);
                        }
                        else if (item.I_LAIHANLX == (int)eLaiHanLX.年度)
                        {
                            var bllT = new YS_LH_NIANTIAOZProcess();
                            int I_LIUCHENGBH = (int)item.I_LIUCHENGBH;
                            int I_LIUCHENGBHLast = "51" == item.S_ZHANDIANBH && 50 == I_LIUCHENGBH ? I_LIUCHENGBH - 20 : I_LIUCHENGBH - 10;
                            drcT = bllT.SelectYS_LH_NIANTIAOZsDynamic("I_JLZT=0 AND S_LAIHANBH='" + item.S_LAIHANBH + "' and I_LIUCHENGBH = '"
                                + I_LIUCHENGBHLast + "'", null).Rows;
                            if (drcT.Count != 1) throw new Exception("records count is not correct");
                            var itemFB = new YS_LH_NIANTIAOZ();
                            itemFB.S_CAOZUORQM = LoginInfo.CurrentUser.ElectronSign;
                            itemFB.D_SHURURQ = DateTime.Now;
                            itemFB.I_JLZT = 0;
                            itemFB.I_LIUCHENGBH = I_LIUCHENGBH;
                            itemFB.I_SHURUR = LoginInfo.CurrentUser.UserID;
                            itemFB.S_ZHANDIANBH = item.S_ZHANDIANBH;
                            itemFB.S_LAIHANBH = item.S_LAIHANBH;
                            itemFB.S_YONGHUBH = item.S_YONGHUBH;
                            itemFB.I_Y = item.I_TZY;
                            itemFB.S_SHENPIYJ = Convert.ToString(drcT[0]["S_SHENPIYJ"]);
                            itemFB.I_M1 = Convert.ToInt32(drcT[0]["I_M1"]);
                            itemFB.I_M2 = Convert.ToInt32(drcT[0]["I_M2"]);
                            itemFB.I_M3 = Convert.ToInt32(drcT[0]["I_M3"]);
                            itemFB.I_M4 = Convert.ToInt32(drcT[0]["I_M4"]);
                            itemFB.I_M5 = Convert.ToInt32(drcT[0]["I_M5"]);
                            itemFB.I_M6 = Convert.ToInt32(drcT[0]["I_M6"]);
                            itemFB.I_M7 = Convert.ToInt32(drcT[0]["I_M7"]);
                            itemFB.I_M8 = Convert.ToInt32(drcT[0]["I_M8"]);
                            itemFB.I_M9 = Convert.ToInt32(drcT[0]["I_M9"]);
                            itemFB.I_M10 = Convert.ToInt32(drcT[0]["I_M10"]);
                            itemFB.I_M11 = Convert.ToInt32(drcT[0]["I_M11"]);
                            itemFB.I_M12 = Convert.ToInt32(drcT[0]["I_M12"]);
                            bllT.InsertOrUpdate(itemFB);
                        }
                        else if (item.I_LAIHANLX == (int) eLaiHanLX.水资源销表)
                        {
                            var bllT = new QS_LH_TONGYONGBZYJProcess();
                            var itemFB = new QS_LH_TONGYONGBZYJ();
                            itemFB.D_SHURURQ = DateTime.Now;
                            itemFB.I_JLZT = 0;
                            itemFB.I_LIUCHENGBH = (int) item.I_LIUCHENGBH;
                            itemFB.I_SHURUR = LoginInfo.CurrentUser.UserID;
                            itemFB.S_LAIHANBH = item.S_LAIHANBH;
                            if (item.I_LIUCHENGBH == (int) eLaiHanLCBH.业务员)
                            {
                                var ucT = Parent.FindControl("UCLHLCSHENPI") as UCLHLCSHENPI;
                                var rsT = ucT.Validate();
                                if (!rsT.IsSuccess)
                                {
                                    cr.Msg = rsT.Msg;
                                    cr.IsSuccess = false;
                                    cr.IsAlertMsg = true;
                                    e.Result = cr.ToString();
                                    return;
                                }

                                itemFB.I_SHENPIJG = 1;
                                itemFB.S_SHENPIYJ = ucT.HuJiSPYJ;
                            }
                            else
                            {
                                var pre = -1;
                                if (item.I_LIUCHENGBH == (int) eLaiHanLCBH.计财科)
                                    pre = (int) eLaiHanLCBH.主任;
                                else
                                    pre = (int) item.I_LIUCHENGBH - 1;
                                drcT = bllT.SelectQS_LH_TONGYONGBZYJsDynamic(
                                        "I_JLZT=0 AND S_LAIHANBH='" + item.S_LAIHANBH + "' AND I_LIUCHENGBH=" + pre,
                                        null)
                                    .Rows;
                                if (drcT.Count == 0) throw new Exception("pre step records is not correct");
                                itemFB.I_SHENPIJG = Convert.ToInt32(drcT[0]["I_SHENPIJG"]);
                                itemFB.S_SHENPIYJ = item.I_LIUCHENGBH == (int) eLaiHanLCBH.处长 ? "同意。" : "拟同意。";
                            }

                            //插入数据库
                            bllT.InsertOrUpdate(itemFB);
                        }
                        else
                        {
                            throw new Exception("other laihanlxs wait to achieve");
                        }

                        //更新来函主信息                        
                        item.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
                        item.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
                        item.I_DANGQIANBZQR = 1;
                        _YS_LH_LAIHANXXBLL.InsertOrUpdate(item);
                    }

                cr.Msg = "已批量确认" + plqrCount + "条来函";
                cr.IsSuccess = true;
                cr.IsAlertMsg = true;
                e.Result = cr.ToString();
            }
        }

        /// <summary>
        ///     修改按钮事件
        /// </summary>
        protected void BtnXG_Click(object sender, EventArgs e)
        {
            //if (this.LaiHanLX == eLaiHanLX.月度)
            //{
            //    UCLHYueDuTZRightInfo LHYueDuTZRightInfo = this.Parent.FindControl("UCLHYueDuTZRightInfo") as UCLHYueDuTZRightInfo;

            //    if (this.BtnXG.Text == "修改")
            //    {
            //        LHYueDuTZRightInfo.SetXiuGai(true);
            //        this.BtnXG.Text = "放弃";
            //        this.BtnTJ.Enabled = false;
            //    }
            //    else
            //    {
            //        LHYueDuTZRightInfo.SetXiuGai(false);
            //        this.BtnXG.Text = "修改";
            //        this.BtnTJ.Enabled = true;
            //    }
            //}

            //else
            //{
            var uc = Parent.FindControl("UCLHLCSHENPI") as UCLHLCSHENPI;
            var ucLeft = Parent.FindControl("UCLHLCLAIHANXX") as UCLHLCLAIHANXX;
            if (BtnXG.Text == "修改")
            {
                if (uc != null) uc.SetShenPiContent(true);
                if (ucLeft != null) ucLeft.SetLaiHanNREnabled(true);
                BtnXG.Text = "放弃";
                BtnTJ.Enabled = false;
                BtnQR.Enabled = true;
            }
            else
            {
                var lhxxT = _YS_LH_LAIHANXXBLL.SelectYS_LH_LAIHANXX(decimal.Parse(LaiHanID));
                //重新加载默认审批量,禁用输入内容
                if (uc != null) uc.LoadInfo(lhxxT);
                if (ucLeft != null) ucLeft.LoadInfo(lhxxT);
                BtnXG.Text = "修改";
                BtnTJ.Enabled = CanTJ();
                BtnQR.Enabled = false;
            }

            //}
        }

        #endregion

        #region 方法

        /// <summary>
        ///     配置控件
        /// </summary>
        public void InitControl()
        {
            LaiHanBH = "";
            YongHuH = "";
            LaiHanID = null;

            BtnXG.Enabled = false;
            BtnQR.Enabled = false;
            BtnTJ.Enabled = false;
            BtnXXXX.Enabled = false;
            BtnYSSF.Enabled = false;
            BtnWJLB.Enabled = false;
            BtnFSTJ.Visible = false;
            if (BusinessTool.WType != 0) BtnYSSF.Visible = false;
            btnZJXB.Visible = false;
            BtnPLQR.Visible = false;
        }

        private bool CanTJ()
        {
            if (LaiHanLC == (int)eLaiHanLCBH.计财科 && LaiHanLX == eLaiHanLX.月度)
            {
                int iSLYM = int.Parse(BLL.BusinessTool.YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString()));
                DataTable dt = _CANSHUBLL.SelectQS_CANSHUBsDynamic(string.Format("i_jlzt=0 and S_MINGCHENG='YueTiaoZSY_YM' and S_ZHI='{0}'", iSLYM), null);
                return dt.Rows.Count > 0;
            }
            return true;
        }

        private void SetDisplay(YS_LH_LAIHANXX lhxx)
        {
            //业务员不显示详细信息按钮
            BtnXXXX.Visible = lhxx.I_LIUCHENGBH > (int) eLaiHanLCBH.业务员;
            //目前为节水科以上步骤开启月调整批量确认功能
            if (lhxx.I_LAIHANLX == (int) eLaiHanLX.月度 || lhxx.I_LAIHANLX == (int) eLaiHanLX.计划变更 || lhxx.I_LAIHANLX == (int)eLaiHanLX.年度)
            {
                if (lhxx.I_LIUCHENGBH >= (int) eLaiHanLCBH.主任) BtnPLQR.Visible = true;
            }
            else if (lhxx.I_LAIHANLX == (int) eLaiHanLX.水资源销表)
            {
                if (lhxx.I_LIUCHENGBH >= (int) eLaiHanLCBH.业务员)
                    BtnPLQR.Visible = true;
                else
                    BtnPLQR.Visible = false;
            }
            else if (LaiHanLX == eLaiHanLX.销表)
            {
                if (lhxx.I_LIUCHENGBH == (int) eLaiHanLCBH.内勤) btnZJXB.Visible = true;
            }
            else if (LaiHanLX == eLaiHanLX.销户)
            {
                if (lhxx.I_LIUCHENGBH == (int) eLaiHanLCBH.内勤) btnZJXH.Visible = true;
            }
        }

        /// <summary>
        ///     提交验证
        /// </summary>
        public ValidateRS Validate()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        ///     清除信息
        /// </summary>
        public void ClearInfo()
        {
            InitControl();
        }

        /// <summary>
        ///     根据来函加载信息
        /// </summary>
        public void LoadInfo(YS_LH_LAIHANXX laihanxx)
        {
            //保存来函号与用户号
            LaiHanBH = laihanxx.S_LAIHANBH;
            YongHuH = laihanxx.S_YONGHUBH;
            LaiHanID = laihanxx.ID.ToString();
            LaiHanLC = (int) laihanxx.I_LIUCHENGBH;
            SetDisplay(laihanxx);
            /* REPLACE BY LAIHANXX I_DANGQIAN 
            //判断当前流程意见是否存在,不存在则禁用修改,存在则启用
            DataRowCollection drc ;
            if(laihanxx.I_LAIHANLX==(int)eLaiHanLX.年度)
            {
                drc = new YS_LH_NIANTIAOZBLL().SelectYS_LH_NIANTIAOZsDynamic("S_LAIHANBH=" + laihanxx.S_LAIHANBH + " and I_LIUCHENGBH=" + laihanxx.I_LIUCHENGBH, null).Rows;
            }
            else if (laihanxx.I_LAIHANLX == (int)eLaiHanLX.基建调整)
            {
                drc = new YS_LH_JIJIANTZBLL().SelectYS_LH_JIJIANTZsDynamic("S_LAIHANBH=" + laihanxx.S_LAIHANBH + " and I_LIUCHENGBH=" + laihanxx.I_LIUCHENGBH, null).Rows;
            }
            else if ((laihanxx.I_LAIHANLX == (int)eLaiHanLX.月度))
            {
                drc = new YS_LH_YUETIAOZProcess().SelectYS_LH_YUETIAOZsDynamic("S_LAIHANBH=" + laihanxx.S_LAIHANBH , null).Rows;           
            }
            else if (laihanxx.I_LAIHANLX == (int)eLaiHanLX.计划变更)
            {
                drc = new YS_LH_JIHUABGBLL().SelectYS_LH_JIHUABGsDynamic("S_LAIHANBH=" + laihanxx.S_LAIHANBH + " and I_LIUCHENGBH=" + laihanxx.I_LIUCHENGBH, null).Rows;
            }
            else if (laihanxx.I_LAIHANLX == (int)eLaiHanLX.分户)
            {
                drc = new YS_LH_FENHUXXBLL().SelectYS_LH_FENHUXXesDynamic("S_LAIHANBH=" + laihanxx.S_LAIHANBH + " and I_JLZT=0", null).Rows;
            }
            else if (laihanxx.I_LAIHANLX == (int)eLaiHanLX.并户)
            {
                drc = new YS_LH_BINGHUProcess().SelectYS_LH_BINGHUsDynamic("S_LAIHANBH=" + laihanxx.S_LAIHANBH + " and I_LIUCHENGBH=" + laihanxx.I_LIUCHENGBH, null).Rows;
            }
            else if (laihanxx.I_LAIHANLX == (int)eLaiHanLX.销户)
            {
                drc = new YS_LH_XIAOHUBLL().SelectYS_LH_XIAOHUsDynamic("S_LAIHANBH=" + laihanxx.S_LAIHANBH + " and I_LIUCHENGBH=" + laihanxx.I_LIUCHENGBH, null).Rows;
            }
            else if (laihanxx.I_LAIHANLX == (int)eLaiHanLX.销表)
            {
                drc = new YS_LH_XIAOBIAOBLL().SelectYS_LH_XIAOBIAOsDynamic("S_LAIHANBH=" + laihanxx.S_LAIHANBH + " and I_LIUCHENGBH=" + laihanxx.I_LIUCHENGBH, null).Rows;
            }
            else if (laihanxx.I_LAIHANLX == (int)eLaiHanLX.退款)
            {
                drc = new YS_LH_TUIKUANBLL().SelectYS_LH_TUIKUANsDynamic("I_DANGQIAN=1 AND S_LAIHANBH=" + laihanxx.S_LAIHANBH + " and I_LIUCHENGBH=" + laihanxx.I_LIUCHENGBH, null).Rows;
            }
            else if (laihanxx.I_LAIHANLX == (int)eLaiHanLX.改户)
            {
                drc = new YS_LH_GAIHUXXBLL().SelectYS_LH_GAIHUXXesDynamic("S_LAIHANBH=" + laihanxx.S_LAIHANBH + " and I_LIUCHENGBH=" + laihanxx.I_LIUCHENGBH, null).Rows;
            }         
            else if ((int)this.LaiHanLX>20)
            {
                drc = new QS_LH_TONGYONGBZYJProcess().SelectQS_LH_TONGYONGBZYJsDynamic("S_LAIHANBH=" + laihanxx.S_LAIHANBH + " and I_LIUCHENGBH=" + laihanxx.I_LIUCHENGBH, null).Rows;
            }
            else
            {
                drc = new DataTable().Rows;
            }*/
            if (laihanxx.I_DANGQIANBZQR == 1)
            {
                BtnXG.Enabled = true;
                BtnQR.Enabled = false;
            }
            else
            {
                BtnXG.Enabled = false;
                BtnQR.Enabled = true;
                //this.BtnXG.Enabled = drc.Count > 0;
                //this.BtnQR.Enabled = !this.BtnXG.Enabled;
            }

            BtnTJ.Enabled = CanTJ();
            BtnXXXX.Enabled = true;
            BtnYSSF.Enabled = true;
            BtnWJLB.Enabled = true;
            //复审修改
            if (FSXG)
            {
                BtnFSTJ.Visible = true;
                BtnTJ.Visible = false;
                BtnXG.Visible = false;
                BtnQR.Visible = false;
            }
        }

        private void UpdateYS_YG_YONGHUXX(string laihanbh)
        {
            var oldyh = new YS_LH_YONGHUXXProcess().SelectYS_LH_YONGHUXXesDynamic(
                string.Format(" I_JLZT=0 and S_LAIHANBH='{0}'", laihanbh), "", null);
            if (oldyh != null && oldyh.Rows.Count > 0)
            {
                var dr = oldyh.Rows[0];
                var col =
                    "S_LIANXIDH='{0}',S_LIANXIR='{1}',S_YONGHUM='{2}',S_YOUBIAN='{3}',S_LIANXIBM='{4}',S_DIZHI='{5}' ";

                var where = "S_YONGHUH='{0}'";
                col = string.Format(col, dr["S_LIANXIDH"], dr["S_LIANXIR"], dr["S_YONGHUM"], dr["S_YOUBIAN"],
                    dr["S_LIANXIBM"], dr["S_DIZHI"]);
                where = string.Format(where, dr["S_YONGHUBH"]);
                new YS_YG_YONGHUXXProcess().UpdateYS_YG_YONGHUXXesDynamic(col, where);
            }
        }

        #endregion
    }
}